﻿
Imports System.Runtime.CompilerServices
Imports osi.root.constants

Public Module _bytes_int
    Sub New()
        k_assert(sizeof_int8 = sizeof_uint8)
        k_assert(sizeof_int16 = sizeof_uint16)
        k_assert(sizeof_int32 = sizeof_uint32)
        k_assert(sizeof_int64 = sizeof_uint64)
    End Sub

    Public Function bytes_bool(ByVal i() As Byte, ByRef o As Boolean, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim v As Byte = 0
        If bytes_byte(i, v, offset) Then
            o = byte_bool(v)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_bool(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim o As Boolean = False
        k_assert(bytes_bool(i, offset, o))
        Return o
    End Function

    Public Function bytes_byte(ByVal i() As Byte, ByRef o As Byte, Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(i) >= offset + sizeof_int8 Then
            o = i(offset)
            offset += sizeof_int8
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_sbyte(ByVal i() As Byte, ByRef o As SByte, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim b As Byte = 0
        If bytes_byte(i, b, offset) Then
            o = byte_sbyte(b)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_uint8(ByVal i() As Byte, ByRef o As Byte, Optional ByRef offset As UInt32 = 0) As Boolean
        Return bytes_byte(i, o, offset)
    End Function

    Public Function bytes_int8(ByVal i() As Byte, ByRef o As SByte, Optional ByRef offset As UInt32 = 0) As Boolean
        Return bytes_sbyte(i, o, offset)
    End Function

    Public Function bytes_byte(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Byte
        Dim o As Byte = 0
        k_assert(bytes_byte(i, offset, o))
        Return o
    End Function

    Public Function bytes_sbyte(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As SByte
        Dim o As SByte = 0
        k_assert(bytes_sbyte(i, o, offset))
        Return o
    End Function

    Public Function bytes_uint8(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Byte
        Return bytes_byte(i, offset)
    End Function

    Public Function bytes_int8(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As SByte
        Return bytes_sbyte(i, offset)
    End Function

    Public Function bytes_int16(ByVal i() As Byte, ByRef o As Int16, Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(i) >= offset + sizeof_int16 Then
            o = BitConverter.ToInt16(i, offset)
            offset += sizeof_int16
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_uint16(ByVal i() As Byte, ByRef o As UInt16, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim s As Int16 = 0
        If bytes_int16(i, s, offset) Then
            o = int16_uint16(s)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_char(ByVal i() As Byte, ByRef o As Char, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim u As UInt16 = 0
        If bytes_uint16(i, u, offset) Then
            o = Convert.ToChar(u)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_int16(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Int16
        Dim o As Int16 = 0
        k_assert(bytes_int16(i, o, offset))
        Return o
    End Function

    Public Function bytes_uint16(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As UInt16
        Dim o As UInt16 = 0
        k_assert(bytes_uint16(i, o, offset))
        Return o
    End Function

    Public Function bytes_char(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Char
        Dim o As Char = Nothing
        k_assert(bytes_char(i, o, offset))
        Return o
    End Function

    Public Function bytes_int32(ByVal i() As Byte, ByRef o As Int32, Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(i) >= offset + sizeof_int32 Then
            o = BitConverter.ToInt32(i, offset)
            offset += sizeof_int32
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_uint32(ByVal i() As Byte, ByRef o As UInt32, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim s As Int32 = 0
        If bytes_int32(i, s, offset) Then
            o = int32_uint32(s)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_int32(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Int32
        Dim o As Int32 = 0
        k_assert(bytes_int32(i, o, offset))
        Return o
    End Function

    Public Function bytes_uint32_(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As UInt32
        Dim o As UInt32 = 0
        k_assert(bytes_uint32(i, o, offset))
        Return o
    End Function

    Public Function bytes_int64(ByVal i() As Byte, ByRef o As Int64, Optional ByRef offset As UInt32 = 0) As Boolean
        If array_size(i) >= offset + sizeof_int64 Then
            o = BitConverter.ToInt64(i, offset)
            offset += sizeof_int64
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_uint64(ByVal i() As Byte, ByRef o As UInt64, Optional ByRef offset As UInt32 = 0) As Boolean
        Dim s As Int64 = 0
        If bytes_int64(i, s, offset) Then
            o = int64_uint64(s)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function bytes_int64(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As Int64
        Dim o As Int64 = 0
        k_assert(bytes_int64(i, o, offset))
        Return o
    End Function

    Public Function bytes_uint64(ByVal i() As Byte, Optional ByRef offset As UInt32 = 0) As UInt64
        Dim o As UInt64 = 0
        k_assert(bytes_uint64(i, o, offset))
        Return o
    End Function
End Module
